/**
 * 基于 axios 封装库
 */

import axios from 'axios'
import router from '@/router'
import storage from '@/utils/storage'
import { Message } from 'element-ui';


var instance = axios.create({
    baseURL: '',
    timeout: 5000
});


// request 拦截器
instance.interceptors.request.use(function (config) {

    let user = storage.get("user");

    config.headers.Version = '3.1.0';
    if (user && user.token) {
        // config.headers.Authorization = `token ${store.state.token}`;
        config.headers.Token = user.token;        
    }

    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// response 拦截器
instance.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
}, function (error) {
    // Do something with response error
    console.log('err' + error);// for debug
    Message({
        message: error.message,
        type: 'error'
    });
    return Promise.reject(error);
});


export default instance;